import fetch from 'isomorphic-fetch';
import cookie from 'react-cookie'
import HostConfig from '../../constants/HostConfig';
import { message } from 'antd';
import { Msg } from '../../constants/MsgBox';

const rootUrl = HostConfig.host;

//const rapUrl = "";
let commonFetch = (method, url, parameters, version, options)=> {
  parameters = parameters ? parameters : {};
  if(Array.isArray(parameters)){
    parameters = parameters.map(item => (item.zoneId = zoneId) && item);
  } else {
    parameters.zoneId = zoneId;
  }

  /*检查网络连接*/
  if(!navigator.onLine) {
    return new Promise(function (resolve, reject) {
      reject({message:'请检查网络连接!'});
    });
  }

  const urlPath = String(url);

  if (!parameters) parameters = {};

  if (options != null) return fetch(rootUrl + urlPath, options);

  let newOptions = ((version == null) ? {
    headers: {
      'Accept': 'application/json',
      // 'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json'
    }
  } : {
    headers: {
      //'Accept': 'application/vnd.souban-v' + version + '+json',
      //'Content-Type': 'application/json'
    }
  });

  newOptions.credentials = 'include';

  if (method === 'get') {
    let paraString = '';

    for (let key in parameters) {
      let value = parameters[key];
      if (typeof value === 'object' || Array.isArray(value)) {
        value = JSON.stringify(value);
      }

      let item = key + '=' + value;
      if (paraString.length == 0) {
        paraString = paraString + item;
      } else {
        paraString = paraString + '&' + item;
      }
    }
    if (paraString.length != 0) {
      url = url + '?' + paraString;
    }
    let wholeUrl = rootUrl + url;

    newOptions.method = 'get';
    return fetch(wholeUrl, newOptions);
  }
  if (method === 'post') {
    newOptions.method = 'post';
    newOptions.body = JSON.stringify(parameters);
    return fetch(rootUrl + urlPath, newOptions);
  }
  if (method === 'put') {
    newOptions.method = 'put';
    newOptions.body = JSON.stringify(parameters);
    return fetch(rootUrl + urlPath, newOptions);
  }
  if (method === 'delete') {
    let paraString = '';

    for (let key in parameters) {
      let value = parameters[key];
      if (typeof value === 'object' || Array.isArray(value)) {
        value = JSON.stringify(value);
      }

      let item = key + '=' + value;
      if (paraString.length == 0) {
        paraString = paraString + item;
      } else {
        paraString = paraString + '&' + item;
      }
    }
    if (paraString.length != 0) {
      url = url + '?' + paraString;
    }
    let wholeUrl = rootUrl + url;

    newOptions.method = 'delete';
    console.log(wholeUrl, newOptions)
    return fetch(wholeUrl, newOptions);
  }

  return fetch(rootUrl + urlPath, options);
};


function dealWithErr(err) {
  switch(err.message) {
    case 'Failed to fetch':
      return {msg: '服务异常,请刷新页面后重试', type: 'warning'};
    default:
      return {msg: err.message, type: 'warning'};
  }
}

export let fetchGet = (url, parameters, version) => {
  return commonFetch('get', url, parameters, version).then((response) => {
    /*if(response.redirected){
        window.location.href = response.url;
    }*/
    return response.json();
  })
  .then((data) => {
    return data;
  })
  .catch(err => {
    return dealWithErr(err);
  });
};

export let fetchPost = (url, parameters, version) => {
  return commonFetch('post', url, parameters, version).then((response) => {
    /*if(response.redirected){
        window.location.href = response.url;
    }*/
    return response.json();
  })
  .then((data) => {
    return data;
  })
  .catch(err => {
    return dealWithErr(err);
  });
};

export let fetchPut = (url, parameters, version) => {
  return commonFetch('put', url, parameters, version).then((response) => {
    /*if(response.redirected){
        window.location.href = response.url;
    }*/
    return response.json();
  })
  .then((data) => {
    return data;
  })
  .catch(err => {
    return dealWithErr(err);
  });
};

export let fetchDelete = (url, parameters, version) => {
  return commonFetch('delete', url, parameters, version).then((response) => {
    /*if(response.redirected){
        window.location.href = response.url;
    }*/
    return response.json();
  })
  .then((data) => {
    return data;
  })
  .catch(err => {
    return dealWithErr(err);
  });
};
export let fetchRapGet = (url, version) => {
  let newOptions = ((version == null) ? {
    headers: {
      'Accept': 'application/json',
      'Accept':'Access-Control-Allow-Origin：*',
      'Content-Type': 'application/json'
    }
  } : {
    headers: {
      //'Accept': 'application/vnd.souban-v' + version + '+json',
      //'Content-Type': 'application/json'
    }
  });
  let wholeUrl = rapUrl + url;
  newOptions.method = 'get';
  console.log(wholeUrl, newOptions)
  return fetch(wholeUrl, newOptions);
};
